import {getSignLogUsage, getSignLogUsageDaily ,getSignLogComprehensive} from '@/base/api/SignLogApi';
import { calculateTrendLine } from "@/base/util/Dates";
export class DataBloc {
    startValue=0
    endValue=0
    optionsSignLogUsage:object={
        color: ['#2d8cf0', '#91CC75', '#ee656e'],
        title: {
            text: '月签到人数统计',
            left: 'center',
            top: 5
        },
        tooltip:{
            trigger: 'axis'
        },
        toolbox: {
            feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        legend: {
            bottom:0,
            data: [ '平均线','数据展示','趋势线']
        },
        grid: [{ left: '5%', top: '10%', width: '90%', height: '80%' }],
        xAxis: [
            {
            name: '日期',
            type: 'category',
            boundaryGap: false,
            }
        ],
        yAxis: {
            name: '人次',
            type: 'value'
        },
        series: [
            {
                name: '平均线',
                type: 'line',
                data: []
            },
            {
                name: '数据展示',
                type: 'line',
                data: []
            },
            {
                name: '趋势线',
                type: 'line',
                markLine: {
                    animation: true,
                    lineStyle: {
                    type: 'dashed',
                    color: 'red'
                    },
                    data: [
                    [
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        },
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        }
                    ]
                    ]
                }
            },
        ]
    }
    
    dateSignLogUsage = [
        new Date(new Date().getTime()-6*30*24*60*60*1000),
        new Date()
    ];
    async getSignLogUsage(){
        let startTime:any=this.dateSignLogUsage[0].getTime()
        let endTime:any=this.dateSignLogUsage[1].getTime()
        return await getSignLogUsage({
            startTime,
            endTime
        }).then((res)=>{
            // console.log(res.data);return;
            if(res.data.length<=0)return
            const dataList:any=[]
            const dataAvg:any=[]
            // 1. 提取num字段为一个数组
            const nums = res.data.map(item => item.num);
            // 2. 求和
            const sum = nums.reduce((total, cur) => total + cur, 0);
            // 3. 求平均值
            const avg = Math.ceil(sum / nums.length);
            res.data.map((item)=>{
                dataList.push([item.month,item.num])
                dataAvg.push([item.month,avg])
            })
            const trendLine=calculateTrendLine(res.data)
            // console.log('斜率:', trendLine.slope);
            // console.log('截距:', trendLine.intercept);
            // console.log('起始点值:', trendLine.startValue);
            // console.log('结束点值:', trendLine.endValue);
            this.startValue=trendLine.startValue
            this.endValue=trendLine.endValue
            this.optionsSignLogUsage['series'][0]['data']=dataAvg
            this.optionsSignLogUsage['series'][1]['data']=dataList
            this.optionsSignLogUsage['series'][2]['markLine']['data'][0][0]['coord'][0]=dataList[0][0]
            this.optionsSignLogUsage['series'][2]['markLine']['data'][0][0]['coord'][1]=this.startValue
            this.optionsSignLogUsage['series'][2]['markLine']['data'][0][1]['coord'][0]=dataList[dataList.length-1][0]
            this.optionsSignLogUsage['series'][2]['markLine']['data'][0][1]['coord'][1]=this.endValue
            console.log(dataList,trendLine,this.optionsSignLogUsage)
        })
    }


    startValueDaily=0
    endValueDaily=0
    optionsSignLogUsageDaily:object={
        color: ['#2d8cf0', '#91CC75', '#ee656e'],
        title: {
            text: '日签到人数统计',
            left: 'center',
            top: 5
        },
        tooltip:{
            trigger: 'axis'
        },
        toolbox: {
            feature: {
            dataView: { show: true, readOnly: false },
            restore: { show: true },
            saveAsImage: { show: true }
            }
        },
        legend: {
            bottom:0,
            data: [ '平均线','数据展示','趋势线']
        },
        grid: [{ left: '5%', top: '10%', width: '90%', height: '80%' }],
        xAxis: [
            {
            name: '日期',
            type: 'category',
            boundaryGap: false,
            }
        ],
        yAxis: {
            name: '人次',
            type: 'value'
        },
        series: [
            {
                name: '平均线',
                type: 'line',
                data: []
            },
            {
                name: '数据展示',
                type: 'line',
                data: []
            },
            {
                name: '趋势线',
                type: 'line',
                markLine: {
                    animation: true,
                    lineStyle: {
                    type: 'dashed',
                    color: 'red'
                    },
                    data: [
                    [
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        },
                        {
                        coord: [0, 0],
                        symbol: 'none'
                        }
                    ]
                    ]
                }
            },
        ]
    }
    txt3=''
    optionsSignLogComprehensive:object={
        color: ['#2d8cf0', '#91CC75'],
        title : {
            text: '签到积分统计',
            left: 'center',
        },
        tooltip : {
            trigger: 'axis'
        },
        legend: {
            data:['签到积分','打卡次数'],
            bottom:5,
        },
        xAxis : [
            {
                type : 'category',
                data : []
            }
        ],
        yAxis : [
            {
                type : 'value'
            }
        ],
        series : [
            {
                name:'签到积分',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name: '平均值'}
                    ]
                }
            },
            {
                name:'打卡次数',
                type:'line',
                data:[],
                markLine : {
                    data : [
                        {type : 'average', name : '平均值'}
                    ]
                }
            }
        ]
    };
    
    dateSignLogUsageDaily = [
        new Date(new Date().getTime()-15*24*60*60*1000),
        new Date()
    ];
    dateSignLogComprehensive = [
        new Date(new Date().getTime()-360*24*60*60*1000),
        new Date()
    ];
    timeUnit=0
    async getSignLogUsageDaily(){
        let startTime:any=this.dateSignLogUsageDaily[0].getTime()
        let endTime:any=this.dateSignLogUsageDaily[1].getTime()
        return await getSignLogUsageDaily({
            startTime,
            endTime
        }).then((res)=>{
            // console.log(res.data);return;
            if(res.data.length<=0)return
            const dataList:any=[]
            const dataAvg:any=[]
            const modifiedData:any=[]
            // 1. 提取num字段为一个数组
            const nums = res.data.map(item => Number(item.count));
            // 2. 求和
            const sum = nums.reduce((total, cur) => total + cur, 0);
            // 3. 求平均值
            const avg = Math.ceil(sum / nums.length);
            res.data.map((item)=>{
                dataList.push([item.created,Number(item.count)])
                dataAvg.push([item.created,avg])
                modifiedData.push({
                    month: item.created, // 提取日期中的月份
                    num: parseInt(item.count) // 将字符串转换成整型
                })
            })
            const trendLine=calculateTrendLine(modifiedData)
            // console.log('斜率:', trendLine.slope);
            // console.log('截距:', trendLine.intercept);
            // console.log('起始点值:', trendLine.startValue);
            // console.log('结束点值:', trendLine.endValue);
            this.startValueDaily=trendLine.startValue
            this.endValueDaily=trendLine.endValue
            this.optionsSignLogUsageDaily['series'][0]['data']=dataAvg
            this.optionsSignLogUsageDaily['series'][1]['data']=dataList
            this.optionsSignLogUsageDaily['series'][2]['markLine']['data'][0][0]['coord'][0]=dataList[0][0]
            this.optionsSignLogUsageDaily['series'][2]['markLine']['data'][0][0]['coord'][1]=this.startValueDaily
            this.optionsSignLogUsageDaily['series'][2]['markLine']['data'][0][1]['coord'][0]=dataList[dataList.length-1][0]
            this.optionsSignLogUsageDaily['series'][2]['markLine']['data'][0][1]['coord'][1]=this.endValueDaily
            console.log(dataList,trendLine,this.optionsSignLogUsageDaily)
        })
    }
    async getSignLogComprehensive(){
        let startTime:any=this.dateSignLogComprehensive[0].getTime()
        let endTime:any=this.dateSignLogComprehensive[1].getTime()
        let timeUnit:any=this.timeUnit
        return await getSignLogComprehensive({
            startTime,
            endTime,
            timeUnit
        }).then((res)=>{
            // console.log(res.data);return;
            if(res.data.specificIntegral.length<=0)return
            const xAxis:any=[]
            const series1:any=[]
            const series2:any=[]
            const modifiedData:any=[]
            // 3. 求平均值
            res.data.specificIntegral.map((item)=>{
                this.timeUnit==0?xAxis.push(item.months):xAxis.push(item.weekstart+'\n'+item.weekend)
                series1.push(item.sum)
                series2.push(item.count)
            })
            console.log(xAxis,series1,series2)
            this.optionsSignLogComprehensive['xAxis'][0]['data']=xAxis
            this.optionsSignLogComprehensive['series'][0]['data']=series1
            this.optionsSignLogComprehensive['series'][1]['data']=series2
            this.txt3=`总赠送积分${res.data.integralCount},总签到次数${res.data.numberCount},除去重复项实际活跃签到人数${res.data.countSignIns}`
        })
    }

}
